import axios from 'axios';
import { ElMessage } from 'element-plus';

const request = axios.create({
  baseURL: 'http://localhost:8080',
});


//添加请求拦截器
request.interceptors.request.use(
  //  请求前的回调
  (config)=>{
    const token = localStorage.getItem('token');
    if (token) {
      //在请求头添加token
      config.headers.Authorization = token;
    }
    return config;
  }
);
import router from '../router';

//添加响应拦截器
request.interceptors.response.use(
  (result)=>{
    //响应前的回调
    return result
  },
  error=>{
    if(error.response.status===401){
      ElMessage.error('登录过期，请重新登录');
      router.push('/login');
    }else{
      ElMessage.error('服务器异常');
    }
    Promise.reject(error);
  }
);




export default request;